package paperfly.game.state;

import java.io.File;
import java.net.MalformedURLException;

import paperfly.game.Player;
import paperfly.game.handler.EnterGameHandler;
import paperfly.map.MapBuilder;

import com.jme.image.Texture;
import com.jme.input.ChaseCamera;
import com.jme.light.DirectionalLight;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Skybox;
import com.jme.scene.state.LightState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.game.state.CameraGameState;

public class InGameState extends CameraGameState {
	private MapBuilder map;
	private Player player;
	private ChaseCamera chaser;
	private EnterGameHandler input;
	private Skybox sky;
	
	public InGameState(String name, Player player, ChaseCamera cam, EnterGameHandler input) {
		super(name);
		this.player = player;
		this.chaser = cam;
		this.chaser.setCamera(super.cam);
		map = new MapBuilder(player, "images/maps/map1/slices",8,DisplaySystem.getDisplaySystem());
		rootNode.attachChild(map.getMap());
		rootNode.attachChild(player);
		rootNode.updateWorldBound();
		this.input = input;
		input.ativarRotacoes();
		buildLight();
		buildSky();
	}
	private void buildLight() {
		DirectionalLight light = new DirectionalLight();
		light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
		light.setAmbient(new ColorRGBA(1f, 1f, 1f, 1.0f));
		light.setDirection(new Vector3f(1, -1, 0));
		light.setEnabled(true);

		/** Attach the light to a lightState and the lightState to rootNode. */
		LightState lightState = DisplaySystem.getDisplaySystem().getRenderer()
				.createLightState();
		lightState.setEnabled(true);
		lightState.attach(light);
		rootNode.setRenderState(lightState);
	}
	@Override
	protected void stateUpdate(float tpf) {
		map.update();
		player.update(tpf);
		input.update(tpf);
		chaser.update(tpf);
		sky.setLocalTranslation(cam.getLocation());
		cam.update();
		super.stateUpdate(tpf);
		Vector3f playerLoc = player.getLocalTranslation();
		if(playerLoc.y+5 <= map.getHeight()){
			playerLoc.y += 10;
			player.setLocalTranslation(playerLoc);
		}
	}
	
	@Override
	protected void stateRender(float tpf) {
		map.getMap().updateRenderState();
		rootNode.updateGeometricState(tpf, false);
		rootNode.updateRenderState();
		super.stateRender(tpf);
	}
	private void buildSky() {
		sky = new Skybox("ceu", 50, 30, 50);
		String dir = "images/skybox/entardecer";
		try {
			Texture north = TextureManager.loadTexture(new File(dir + "/1.jpg")
					.toURL(), Texture.MM_LINEAR, Texture.FM_LINEAR);
			Texture south = TextureManager.loadTexture(new File(dir + "/3.jpg")
					.toURL(), Texture.MM_LINEAR, Texture.FM_LINEAR);
			Texture east = TextureManager.loadTexture(new File(dir + "/2.jpg")
					.toURL(), Texture.MM_LINEAR, Texture.FM_LINEAR);
			Texture west = TextureManager.loadTexture(new File(dir + "/4.jpg")
					.toURL(), Texture.MM_LINEAR, Texture.FM_LINEAR);
			Texture up = TextureManager.loadTexture(new File(dir + "/6.jpg")
					.toURL(), Texture.MM_LINEAR, Texture.FM_LINEAR);
			sky.setTexture(Skybox.NORTH, north);
			sky.setTexture(Skybox.WEST, west);
			sky.setTexture(Skybox.SOUTH, south);
			sky.setTexture(Skybox.EAST, east);
			sky.setTexture(Skybox.UP, up);
			sky.preloadTextures();
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
//		rootNode.attachChild(sky);
//		rootNode.updateRenderState();
	}
}
